package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    BrandMapper brandMapper;

    @Override
    //查询品牌分类
    public List<CategoryBrand> selectCategoryBrandList() {
        return baseMapper.selectCategoryBrandList();
        //return categoryBrandMapper.selectCategoryBrandList();
    }

    @Override
    public void saveCategoryBrand(CategoryBrand categoryBrand) {
        //根据品牌id+分类id查询是否存在
        LambdaQueryWrapper<CategoryBrand> categoryBrandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryBrandLambdaQueryWrapper.eq(CategoryBrand::getBrandId,categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId());

        //判断品牌是否存在
        long count = count(categoryBrandLambdaQueryWrapper);
        if(count>0){
            throw  new SecurityException("品牌已存在");
        }



        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        save(categoryBrand);
    }

/*
    //方式一：采用wrapper查询
    @Override
    public List<Brand> brandList(Long categoryId) {
        List<CategoryBrand> categoryBrandList =
                this.list(new LambdaQueryWrapper<CategoryBrand>()
                        .eq(CategoryBrand::getCategoryId, categoryId)
                        .select(CategoryBrand::getBrandId)
                );
        if(!CollectionUtils.isEmpty(categoryBrandList)){
            //采用Stream流获取品牌ID列表
            //List<Long> brandIdList = categoryBrandList.stream()
            //        //映射，处理流中每一项数据 由CategoryBrand类转为Long类型
            //        //Function函数式接口：T入参类型  R出参类型
            //        .map(categoryBrand -> {
            //            return categoryBrand.getBrandId();
            //        }).collect(Collectors.toList());
            List<Long> brandIdList = categoryBrandList.stream()
                    .map(CategoryBrand::getBrandId)
                    .collect(Collectors.toList());
            //根据品牌ID列表查询品牌列表
            List<Brand> brandList = brandMapper.selectBatchIds(brandIdList);
            return brandList;

        }
        return null;
    }
*/

    //方式二：动态SQL
    @Override
    public List<Brand> brandList(Long categoryId) {
        return baseMapper.brandList(categoryId);
    }




}
